package com.melodyframe.stage.spring;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.context.ApplicationContext;

import com.melodyframe.stage.annotation.AnnotationStage;
import com.melodyframe.stage.annotation.Component;
import com.melodyframe.stage.annotation.Stage;
import com.melodyframe.stage.core.renderer.ViewRenderer;

public abstract class AbstractSpringStage extends AnnotationStage {

	@PostConstruct
	public void init() {
		ApplicationContext applicationContext = this.getApplicationContext();
		if (null == this.getRenderer()) {
			if (applicationContext.containsBean("viewRenderer")) {
				ViewRenderer viewRenderer = (ViewRenderer) applicationContext
						.getBean("viewRenderer");
				this.setRenderer(viewRenderer);
			}
		}
		Map<String, Object> stageBeanMap = applicationContext
				.getBeansWithAnnotation(Stage.class);
		for (Object bean : stageBeanMap.values()) {
			if (bean instanceof com.melodyframe.stage.core.Stage) {
				this.addNamespace((com.melodyframe.stage.core.Stage) bean);
			}
		}
		Map<String, Object> componentBeanMap = applicationContext
				.getBeansWithAnnotation(Component.class);
		for (Object bean : componentBeanMap.values()) {
			this.addComponentBean(bean);
		}
	}

	public abstract ApplicationContext getApplicationContext();

}
